<template>
  <div class="background">
    <div class="top">
      <div style=" font-size: 40px;color: #eaeaea;">病房管理系统
        <HeadTime></HeadTime>
      </div>
    </div>
    <div style="display: flex ;justify-content: space-between">
      <div>
        <img src="../assets/Doctor.png" style="height: 250px;width: 230px;margin: 100px 0 0 280px" alt=""/>
      </div>
      <div
          style="width: 380px;padding:0 20px 0 20px;position:absolute;right:250px;top:110px;box-shadow: 0 0 25px #cac6c6;border: 1px #eaeaea ">
        <div style="color: #cccccc;font-size: 30px;text-align: center;padding: 30px 0">欢迎登陆</div>
        <el-form
            :rules="rules"
            v-loading="loading"
            element-loading-text="登陆中..."
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(0, 0, 0, 0.8)"
            ref="loginForm"
            :model="loginForm"
            class="loginContainer"
        >
          <!-- 用户名 -->
          <el-form-item prop="username">
            <el-input
                prefix-icon="el-icon-user-solid"
                type="text"
                auto-complete="false"
                v-model="loginForm.username"
                placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input
                prefix-icon="el-icon-lock"
                type="password"
                auto-complete="false"
                v-model="loginForm.password"
                placeholder="请输入密码"
            ></el-input>
          </el-form-item>
          <el-form-item prop="validCode">
            <div style="display: flex">
              <el-input
                  prefix-icon="el-icon-key"
                  auto-complete="false"
                  v-model="loginForm.validCode" style="width: 50%"
                  placeholder="请输入验证码"></el-input>
              <ValidCode @input="createValidCode"></ValidCode>
            </div>
          </el-form-item>
          <el-checkbox v-model="checked" class="loginRemember">记住我</el-checkbox>
          <!-- 登录按钮 -->
          <el-button type="primary" style="width: 100%" @click="submitLogin">登录</el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import HeadTime from "@/components/HeadTime";
import ValidCode from "@/components/ValidCode";

export default {
  name: 'Login',
  components: {
    HeadTime,
    ValidCode
  },
  data() {
    return {

      loginForm: {
        username: 'admin',
        password: '123',
        validCode: ''
      },
      checked: true,
      rules: {
        username: [
          {required: true, message: '用户名不能为空', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '密码不能为空', trigger: 'blur'},
        ],
        validCode: [
          {
            required: true, message: '请输入验证码', trigger: 'blur',
          },
        ],
      },
      loading: false,
    }
  },
  methods: {
    //接受验证码组件提交的4位验证码
    createValidCode(data) {
      this.validCode = data
    },
    submitLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          // toLowerCase-----对验证码小写处理
          if (this.loginForm.validCode.toLowerCase() !== this.validCode.toLowerCase()) {
            this.$message({
              message: '验证码错误!',
              type: 'error'
            })
          } else {
            this.loading = true
            this.postRequest('/login', this.loginForm, 'form').then((resp) => {
              if (resp) {
                this.loading = false
                // 存储用户token
                const tokenStr = resp.obj.tokenHead + resp.obj.token
                window.sessionStorage.setItem('tokenStr', tokenStr)
                //跳转首页
                let path = this.$route.query.redirect
                this.$router.replace(
                    path == '/' || path == undefined ? '/home' : path
                )
              }
              if (resp == undefined) {
                this.loading = false
              }
            })
          }
        } else {
          this.$message.error('你还有未填写的信息')
          return false
        }
      })
    },
  },
}
</script>

<style>
.background {
  background: url("../assets/LoginBg.jpg") no-repeat;
  width: 100%;
  height: 100%;
  background-size: cover;
  position: fixed;
}

.top {
  display: flex;
  justify-content: space-between;
  color: #cccccc;
  border-bottom: 1px solid #cccccc;
  text-align: left;
  width: 100%;
  margin-left: 30px;
}

.loginRemember {
  text-align: left;
  margin: 0px 0px 15px 0px;
}
</style>